/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import comboboxview.DesignationCbview;
import dao.DepartmentDAO;
import dao.DesignationDAO;
import dao.EmployeeDAO;
import dao.JobrotationDAO;
import data.Departments;
import data.Designation;
import data.Designationweightage;
import data.DesignationweightageId;
import data.Employees;
import data.Jobrotation;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.JTextField;
import javax.swing.table.TableModel;
import view.ManageDesignation;

/**
 *
 * @author Hùng
 */
public class DesignationBO extends Error {

	String Success;

	public DesignationBO() {
		Success = null;
	}

	public DesignationBO(String Success) {
		this.Success = Success;
	}

	public String getSuccess() {
		return Success;
	}

	public void setSuccess(String Success) {
		this.Success = Success;
	}

	public List<Designation> showAllDesignation() {

		List<Designation> ldesign = new DesignationDAO().getAllDesignation();
		return ldesign;

	}

	public DesignationCbview showAllDesignationInCombobox() {

		List<Designation> ldesign = new DesignationDAO().getAllDesignation();
		DesignationCbview model = new DesignationCbview(ldesign);
		return model;

	}

	/**
	 *
	 * @param txtDesign
	 * @param txtIdDesign
	 */
	public void registerDesignation(JTextField txtDesign, String txtIdDesign) throws Error {
		if (txtIdDesign.equals("")) {
			if (!txtDesign.getText().equals("")) {
				Designation tmpD = new DesignationDAO().getDesignationbyDesignation(txtDesign.getText());
				if (tmpD == null) {
					Designation tmpD1 = new Designation(
							"D" + Calendar.getInstance().getTimeInMillis(),
							txtDesign.getText(),
							null, null, null, null, null);
					boolean flag = new DesignationDAO().executeInsertHQLQuery(tmpD1);
					if (flag == true) {
						Success = "Register Designation SuccessFully";
					} else {
						throw new Error("Register Designation Fail");
					}
				} else {
					throw new Error("Your Designation is existed.");
				}
			} else {
				throw new Error("Please,check your Designation");
			}
		} else {
			throw new Error("You are Editing.Please, Save your information is edited");
		}
	}

	public void removeDesignation(int[] selectedRows, String txtIdDesign, TableModel model) {
		if (txtIdDesign.equals("")) {
			if (selectedRows.length > 0) {
				boolean flag = false;
				List<Designation> lDesign = new ArrayList<Designation>();
				for (int i = 0; i < selectedRows.length; i++) {
					String idDesign = (String) model.getValueAt(selectedRows[i], 0);
					Designation tmpD = new DesignationDAO().getDesignationbyDidDesign(idDesign);
//                    List<Employees> lEmp = new EmployeeDAO().getEmployeesbyIdDesign(idDesign);
					if (tmpD.getEmployeeses().size() > 0) {
						throw new Error("Remove Designation Fail.Please,Delete List Employee's details of this Designation");
					}
//                    List<Jobrotation> lJob = new JobrotationDAO().getJobrorationbyIdDesign(idDesign);
					if (tmpD.getJobrotations().size() > 0) {
						throw new Error("Remove Designation Fail.Please,Delete List Jobrotation's details of this Designation");
					}
					if (tmpD.getVacancieses().size() > 0) {
						throw new Error("Remove Designation Fail.Please,Delete List Vacancy's details of this Designation");

					}
					if (tmpD.getDesignationweightages().size() > 0) {
						throw new Error("Remove Designation Fail.Please,Delete List Designationweightages detail of this Designation");

					}
					if (tmpD.getDesiglayers().size() > 0) {
						throw new Error("Remove Designation Fail.Please,Delete List Desiglayers detail of Designation");

					}
					lDesign.add(tmpD);

				}
				for (Designation a : lDesign) {
					new DesignationDAO().executeDeleteHQLQuery(a);
				}

				Success = "Delete Designations SuccessFully";



			} else {
				throw new Error("Please,Select Rows on Table to delete");
			}
		} else {
			throw new Error("You are Editing.Please, Save your information is edited");
		}
	}

	public void showInformationofAnDesign(int[] selectedRow, JTextField txtDesign, JTextField txtIdDesign, TableModel model) {
		if (selectedRow.length == 1) {
			Designation tmpD = new DesignationDAO().getDesignationbyDesignation((String) model.getValueAt(selectedRow[0], 1));
			ManageDesignation.setsDesign(tmpD);
		} else {
			throw new Error("Please,Select only Row on Table to Edit");
		}
	}

	public void editDesignation(String design, String iddesign) {

		Designation tmpD = new DesignationDAO().getDesignationbyDidDesign(iddesign);
		Designation tmpD1 = new DesignationDAO().getDesignationbyDesignation(design);
		if (tmpD1 == null || design.equals(tmpD.getDesignation())) {
			tmpD.setDesignation(design);
			boolean flag = new DesignationDAO().executeUpdateHQLQuery(tmpD);
			if (flag == true) {
				Success = "Edit Designations SuccessFully";
			}
		} else {
			throw new Error("Your Designation is existed.");
		}


	}
	//hieu

	public ComboBoxModel getComboBox() {
		DesignationDAO desDAO = new DesignationDAO();
		return desDAO.getComboBox();
	}

	public void actionCreateDesignationWeightage(String department, String designationid, String weightage) {
		if (department.equals("-----None-----") || designationid.equals("-----None-----") || weightage.length() == 0||!isNumeric(weightage)) {
			setSuccess("Please check your information!");
		} else {
			Designationweightage dw = new Designationweightage();
			Departments deptmp = new DepartmentDAO().getDepartmentByTagName(department);
			Designation des = new DesignationDAO().getDesignationbyDidDesign(designationid);//null
			dw.setDepartments(deptmp);
			dw.setDesignation(des);
			dw.setWeightage(Integer.parseInt(weightage));
			String tmp1 = des.getDesignationid();
			DesignationweightageId desId = new DesignationweightageId(tmp1, deptmp.getDeptno());
			dw.setId(desId);
			if (new DesignationDAO().executeInsertHQLQuery(dw)) {
				setSuccess("Insert your infomation has been successfull!");
			} else {
				setSuccess("Object is already exist!");
			}

		}
	}

	public boolean actionUpdateDesignationWeightage(String dept, String designationid, String weight) {// chưa bắt lỗi chỗ này nè
		int weightagetmp =0;
		try {
			weightagetmp = Integer.parseInt(weight);
		} catch (NumberFormatException ee) {
			setSuccess(ee.getMessage());
			return false;
		}
		if (weight.length() == 0) {
			setSuccess("Please check your infomation!");
			return false;
		} else {
			Designationweightage dw = new Designationweightage();
			Departments deptmp = new DepartmentDAO().getDepartmentByTagName(dept);
			Designation des = new DesignationDAO().getDesignationbyDidDesign(designationid);//null
			dw.setDepartments(deptmp);
			dw.setDesignation(des);
			dw.setWeightage(Integer.parseInt(weight));
			//update 
			DesignationweightageId desId = new DesignationweightageId(des.getDesignationid(), deptmp.getDeptno());
			dw.setId(desId);
			new DesignationDAO().executeUpdateHQLQuery(dw);
			setSuccess("Update has been successfully!");
			return true;
		}
	}

	public TableModel actionShowTableDesignationWeightage() {
		return new DesignationDAO().showTableDesignationWeightage();
	}
	public boolean actionDelete(String designationid, String dept, String weight) {
			Designationweightage dw = new Designationweightage();
			Departments deptmp = new DepartmentDAO().getDepartmentByTagName(dept);
			Designation des = new DesignationDAO().getDesignationbyDidDesign(designationid);//null
			dw.setDepartments(deptmp);
			dw.setDesignation(des);
			dw.setWeightage(Integer.parseInt(weight));
			//update 
			DesignationweightageId desId = new DesignationweightageId(des.getDesignationid(), deptmp.getDeptno());
			dw.setId(desId);
			return new DesignationDAO().executeDeleteHQLQuery(dw);
	}
	public boolean isNumeric(String s) {
		return s.matches("[-+]?\\d*\\.?\\d+");
	}
}
